//
//  DYZMineController.m
//  DYZCars
//
//  Created by MF on 16/8/18.
//  Copyright © 2016年 MF. All rights reserved.
//

#import "DYZMineController.h"
#import "DYZPersonInfoController.h"
#import "DYZMyFocusController.h"
#import "DYZMyDealController.h"
#import "DYZMyCollectController.h"
#import "DYZSystemSettingController.h"
#import "DYZMySourceController.h"
#import "DYZMyMessageController.h"
#import "DYZMyFindCarController.h"
#import "DYZNetworkManager.h"
#import "DYZPersonInfo.h"

@interface DYZMineController ()
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *depLabel;
@property (weak, nonatomic) IBOutlet UIImageView *iconView;

@property (nonatomic, strong)DYZPersonInfo *info;
@end

@implementation DYZMineController
- (IBAction)myCheyuanbutton:(id)sender {
    NSLog(@"我的车源");
    DYZMySourceController *vc = [[DYZMySourceController alloc] init];
    [self.navigationController pushViewController:vc animated:true];
    
    
}
- (IBAction)myMessageButton:(id)sender {
        NSLog(@"我的消息");
    DYZMyMessageController *vc = [[DYZMyMessageController alloc] init];
      [self.navigationController pushViewController:vc animated:true];
    
}
- (IBAction)myXuncheButton:(id)sender {
        NSLog(@"我的寻车");
    DYZMyFindCarController *vc = [[DYZMyFindCarController alloc] init];
    [self.navigationController pushViewController:vc animated:true];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"注销" style:UIBarButtonItemStylePlain target:self action:@selector(logout:)];

   
     [self userInfo];
}
- (void)setupUserInfo {

    self.nameLabel.text = self.info.name;
    self.depLabel.text = self.info.dep;
    [self.iconView sd_setImageWithURL:[NSURL URLWithString:self.info.photo]];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
   
}
- (void)logout:(UIButton *)sender {
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"提示" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确认退出" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        [self presentViewController:[[NSClassFromString(@"DYZLoginViewController") alloc] init] animated:(true) completion:nil];
    }];

     UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil];
    [alertVC addAction:action1];
    [alertVC addAction:action2];
  
    [self presentViewController:alertVC animated:true completion:nil];
}

#pragma mark - UITableView data source

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"%zd",indexPath.row);
    if (indexPath.row == 0) {
        UIStoryboard *sb = [UIStoryboard storyboardWithName:@"personInfo" bundle:nil];
        DYZPersonInfoController *vc = [sb instantiateInitialViewController];
        vc.info = self.info;
        [self.navigationController pushViewController:vc animated:true];
    } else if(indexPath.row == 2 ) {
//        我的关注
        DYZMyFocusController *vc = [[DYZMyFocusController alloc] init];
        [self.navigationController pushViewController:vc animated:true];
    }else if(indexPath.row == 3 ) {
//        寻车交易
        DYZMyDealController *vc = [[DYZMyDealController alloc] init];
        [self.navigationController pushViewController:vc animated:true];
    }else if(indexPath.row == 4 ) {
//        我的收藏
        DYZMyCollectController *vc = [[DYZMyCollectController alloc] init];
        [self.navigationController pushViewController:vc animated:true];
        
    }else if(indexPath.row == 5 ) {
//        系统设置,完成
        DYZSystemSettingController *vc = [[DYZSystemSettingController alloc] init];
        [self.navigationController pushViewController:vc animated:true];
    }
}


- (void)userInfo{
    
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    
    [param setValue:@"1" forKey:@"accountid"];
    
    
    [DYZNetworkManager postUrlString:@"http://iosapi.itcast.cn/car/personinfo.json.php" withParam:param withSuccessBlock:^(id data) {
//        NSLog(@"%@",data[@"data"]);
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            DYZPersonInfo *info = [[DYZPersonInfo alloc] init];
            [info setValuesForKeysWithDictionary:data[@"data"]];
            self.info = info;
             [self setupUserInfo];
            
        }];
    } withFailedBlock:^(NSError *error) {
        NSLog(@"%@",error);
    } withErrorBlock:^(NSString *message) {
        NSLog(@"%@",message);
    }];
}
@end
